package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyyCwFzhsye;
import cn.stylefeng.guns.sys.modular.system.entity.BtyyCwSyfs;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyyCwFzhsyeMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyyCwSyfsMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class BtySyfsService extends ServiceImpl<BtyyCwSyfsMapper, BtyyCwSyfs> {
    @Resource
    private BtyyCwSyfsMapper syfsMapper;

    /**
     * 获取辅助核算余额列表
     */
    public Page<Map<String, Object>> list(CwbbParam cwbbParam) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page,cwbbParam);
    }
    /**
     * 获取收银方式列表（不分页）
     */
    public List<BtyyCwSyfs> listExportSyfs(CwbbParam cwbbParam) {

        return this.baseMapper.listExportSyfs(cwbbParam);
    }
    /**
     * 导入收银方式excel
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchImportSyfs(String fileName, MultipartFile file,String dm) throws Exception {
        boolean notNull = false;
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }

        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        //excel中多个sheet表，获取所有sheet表的数量
        int sheets = wb.getNumberOfSheets();
        for (int i = 0; i < sheets; i++) {
            Sheet sheet = wb.getSheetAt(i);
            //获取业务日期
            Row rowss = sheet.getRow(1);
            String rows=rowss.getCell(0).toString().replaceAll("\\s*", "");
            String ywrq=rows.substring(rows.indexOf(":") + 1).substring(0,7);
            List<BtyyCwSyfs>list=baseMapper.selectList(new QueryWrapper<BtyyCwSyfs>().eq("dm",dm)
                    .eq("ywrq",ywrq));
            if(list.size()>0){
                baseMapper.delete(new QueryWrapper<BtyyCwSyfs>().eq("dm",dm)
                        .eq("ywrq",ywrq));
            }

            if (sheet != null) {
                notNull = true;
            }

            int lastRowNum = sheet.getLastRowNum();
            for (int r = 3; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                for(int m=1;m<13;m++){
                    Cell cell = row.getCell(m);
                    if (ToolUtil.isNotEmpty(cell)) {
                        cell.setCellType(CellType.STRING);
                    }
                }
                String rq = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();
                String syfsbm = row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue();
                String syfsmc = row.getCell(3) == null ? "" : row.getCell(3).getStringCellValue();
                String kmbm = row.getCell(4) == null ? "" : row.getCell(4).getStringCellValue();
                String kmmc = row.getCell(5) == null ? "" : row.getCell(5).getStringCellValue();
                String pzzh = row.getCell(6) == null ? "" : row.getCell(6).getStringCellValue();
                String zy = row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue();
                String dfkm = row.getCell(8) == null ? "" : row.getCell(8).getStringCellValue();
                String jf = row.getCell(9) == null ? "" : row.getCell(9).getStringCellValue();
                String df = row.getCell(10) == null ? "" : row.getCell(10).getStringCellValue();
                String fx = row.getCell(11) == null ? "" : row.getCell(11).getStringCellValue();
                String ye = row.getCell(12) == null ? "" : row.getCell(12).getStringCellValue();

                BtyyCwSyfs syfs =new BtyyCwSyfs();
                syfs.setId(UUIDUtils.getId());
                syfs.setDm(dm);
                syfs.setYwrq(ywrq);
                syfs.setRq(rq);
                syfs.setSyfsbm(syfsbm);
                syfs.setSyfsmc(syfsmc);
                syfs.setKmbm(kmbm);
                syfs.setKmmc(kmmc);
                syfs.setPzzh(pzzh);
                syfs.setZy(zy);
                syfs.setDfkm(dfkm);
                syfs.setJf(jf);
                syfs.setDf(df);
                syfs.setFx(fx);
                syfs.setYe(ye);
                syfsMapper.insert(syfs);
            }
        }
        return notNull;
    }

}
